import 'dart:io';
import '../universal_platform.dart';

// Platform detection logic for environments where dart:io is available
UniversalPlatformType? _cachedPlatform;

UniversalPlatformType get currentUniversalPlatform {
  if (_cachedPlatform != null) {
    return _cachedPlatform!;
  }
  
  try {
    if (Platform.isWindows) {
      _cachedPlatform = UniversalPlatformType.Windows;
    } else if (Platform.isFuchsia) {
      _cachedPlatform = UniversalPlatformType.Fuchsia;
    } else if (Platform.isMacOS) {
      _cachedPlatform = UniversalPlatformType.MacOS;
    } else if (Platform.isLinux) {
      _cachedPlatform = UniversalPlatformType.Linux;
    } else if (Platform.isIOS) {
      _cachedPlatform = UniversalPlatformType.IOS;
    } else if (Platform.isAndroid) {
      _cachedPlatform = UniversalPlatformType.Android;
    } else {
      // Check for OHOS if supported
      try {
        if (Platform.isOhos) {
          _cachedPlatform = UniversalPlatformType.Ohos;
        } else {
          _cachedPlatform = UniversalPlatformType.Android; // Default fallback
        }
      } catch (e) {
        _cachedPlatform = UniversalPlatformType.Android; // Default fallback
      }
    }
  } catch (e) {
    // If there's any issue with Platform detection, default to Android
    _cachedPlatform = UniversalPlatformType.Android;
  }
  
  return _cachedPlatform!;
}
